#nullable disable
namespace Crux.Core.Data;
/// <summary>
/// Pageable
/// </summary>
public class Pageable<T>
{
    /// <summary>
    ///
    /// </summary>
    /// <param name="option"></param>
    /// <param name="data"></param>
    /// <param name="totalCount"></param>
    public Pageable(
        IQueryable<T> data,
        PageOption option,
        int totalCount = 0) : this(data, option.PageIndex, option.PageSize, totalCount)
    {

    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <param name="data"></param>
    /// <param name="totalCount"></param>
    public Pageable(
        IQueryable<T> data,
        int? pageIndex,
        int? pageSize,
        int totalCount) :
        this(pageIndex, pageSize, totalCount)
    {
        this.Data = data;
    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <param name="totalCount"></param>
    public Pageable(
        int? pageIndex,
        int? pageSize,
        int totalCount = 0)
    {
        this.PageIndex = pageIndex;
        this.PageSize = pageSize;
        this.TotalCount = totalCount;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="option"></param>
    /// <param name="totalCount"></param>
    public Pageable(PageOption option, int totalCount = 0)
        : this(option.PageIndex, option.PageSize, totalCount)
    {

    }
    /// <summary>
    ///
    /// </summary>
    private Pageable()
    {
    }
    /// <summary>
    ///
    /// </summary>
    public IQueryable<T> Data { get; set; }
    /// <summary>
    ///
    /// </summary>
    public int PageCount
    {
        get
        {
            if (!PageSize.HasValue)
            {
                return TotalCount;
            }
            if (0 == PageSize)
            {
                return 0;
            }
            int pageCount = TotalCount / PageSize.Value;

            if (0 != TotalCount % PageSize)
                pageCount++;

            return pageCount;
        }
    }
    /// <summary>
    ///
    /// </summary>
    public int? PageIndex { get; init; }
    /// <summary>
    ///
    /// </summary>
    public int? PageSize { get; init; }
    /// <summary>
    ///
    /// </summary>
    public int TotalCount { get; init; }
}